﻿// // -------------------------------------------------------------------------------------
// // Copyright 2012 ELEN7046 Group 2
// // 
// // Last Modified on: 2012-06-10
// // 
// // Contributors:
// // 
// // Amith Sewnarain
// // Elmar Vosloo
// // Jason Thorne
// // Satish Jewan
// // 
// // Wits University 
// // School of Electrical & Information Engineering
// // Software Engineering Tools and Techniques
// // 
// // -------------------------------------------------------------------------------------
// // 
// // This file is part of the DishDash Event Visualization System.
// // 
// // DishDash is free software: you can redistribute it and/or modify
// // it under the terms of the GNU General Public License as published by
// // the Free Software Foundation, either version 3 of the License, or
// // (at your option) any later version.
// // 
// // DishDash is distributed in the hope that it will be useful,
// // but WITHOUT ANY WARRANTY; without even the implied warranty of
// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// // GNU General Public License for more details.
// // 
// // You should have received a copy of the GNU General Public License
// // along with DishDash.  
// // If not, see <http://www.gnu.org/licenses/>.
// // -------------------------------------------------------------------------------------

var dishdash = dishdash || {};
dishdash.graphs = dishdash.graphs || { };

dishdash.graphs.timeGraph = function(targetId, widgetConfig) {
    this.renderTarget = document.getElementById(targetId);
    this.configuration = widgetConfig;
    this.categoryService = new dishdash.services.EventFetchingClientSideService();
    var graph = null;
    var self = this;

    this.draw = function(data) {

        graph = Flotr.draw(this.renderTarget, data, {
            xaxis: {
                showLabels: true,
                minorTickFreq: 4,
                title: "Time"
            },
            yaxis: {
                showLabels: true,
                title: "Event Count"
            },
            grid: {
                minorVerticalLines: true
            },
            legend: {
                position: 'se',
                backgroundColor: '#D2E8FF'
            },
            HtmlText: false,
            title: "Event Data by Precision: " + this.configuration.precision
        });
    };

    this.start = function() {

        var now = new Date();
        var startTime = new Date(now - (this.configuration.samplePeriod * 1000));

        var request = {
            StartTime: startTime,
            Precision: this.configuration.precision,
            Categories: this.configuration.events
        };
        this.categoryService.GetCategoryDataByPrecision(request, this.serviceCallback);
    };

    this.serviceCallback = function(response) {

        var metaData = {
            title: ""
        };
        
        var dataPoints;
        if (!response.Error && response !== undefined && response.Categories[0] !== undefined && response.Categories[0].PrecisionDocuments[0] !== undefined) {
            dataPoints = [];
            for (var i = 0; i < response.Categories.length; i++) {
                var dataPoint = [];
                for (var j = 0; j < response.Categories[i].PrecisionDocuments.length; j++) {
                    dataPoint.push([j,
                            response.Categories[i].PrecisionDocuments[j].Sum]);
                }

                dataPoints.push({ data: dataPoint, label: response.Categories[i].CategoryName });


            }
        } else {
            dataPoints = {
                data: [0, 0],
                label: ""
            };
            metaData.title = "No Data Available";
        }

        self.draw(dataPoints);

        setTimeout(function() {
            self.start();
        }, self.configuration.refreshPeriod * 1000);
    };
};